/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.support.requestfactory;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.web.bindery.requestfactory.server.ServiceLayerDecorator;
import com.google.web.bindery.requestfactory.shared.Locator;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.Service;
import com.google.web.bindery.requestfactory.shared.ServiceLocator;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author Christophe De Blende
 * @since 02/01/13
 */
public class GuiceServiceLayerDecorator extends ServiceLayerDecorator {

	private final Validator validator;

	private final Injector injector;

	@Inject
	protected GuiceServiceLayerDecorator(final Injector injector, final Validator validator) {
		super();
		this.injector = injector;
		this.validator = validator;
	}

	@Override
	public <T extends Locator<?, ?>> T createLocator(Class<T> clazz) {
		return injector.getInstance(clazz);
	}

	@Override
	public Object createServiceInstance(Class<? extends RequestContext> requestContext) {
		Class<? extends ServiceLocator> serviceLocatorClass = getTop().resolveServiceLocator(requestContext);
		if (serviceLocatorClass != null) {
			return injector.getInstance(serviceLocatorClass).getInstance(requestContext.getAnnotation(Service.class).value());
		} else {
			return null;
		}
	}

	/**
	 * Invokes JSR 303 validator on a given domain object.
	 *
	 * @param domainObject the domain object to be validated
	 * @param <T>          the type of the entity being validated
	 * @return the violations associated with the domain object
	 */
	@Override
	public <T> Set<ConstraintViolation<T>> validate(T domainObject) {
		return validator.validate(domainObject);
	}

}

